home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / afloat.zip / FDEC2BIN.ASM < prev    next >
Assembly Source File  |  1988-03-15  |  3KB  |  137 lines

  1.     PAGE ,132
  2. ;----------------------------------------------------------
  3. ; FDECTOBIN
  4. ;
  5. ; Copyright Bob Kline 1988
  6. ;
  7. ; Purpose:
  8. ;    convert decimal components to 4-byte real
  9. ;
  10. ; Input:
  11. ;       DX:AX contains mantissa with decimal point
  12. ;       understood to be at end; good to 7-8 places
  13. ;       of precision; CX contains sign in low bit;
  14. ;       BX contains signed, unbiased decimal exponent
  15. ;
  16. ; Output:
  17. ;       DX:AX contains binary floating point value
  18. ;       in IEEE format
  19. ;
  20. ; Other registers affected:
  21. ;    BX, CX, DI, SI
  22. ;
  23. ; Other procedures called:
  24. ;       LDIV10 -- divide 32-bit unsigned integer by 10
  25. ;----------------------------------------------------------
  26.         PUBLIC  FDECTOBIN
  27.         EXTRN   LDIV10:PROC
  28.  
  29.     .MODEL    SMALL
  30.  
  31. BINARYEXPONENT    EQU    SI
  32. DECIMALEXPONENT EQU    DI
  33. REMAINDER    EQU    BX
  34.  
  35.     .CODE
  36.  
  37. FDECTOBIN PROC
  38.  
  39. ; get the sign out of the way -- we won't need it until the end
  40.     PUSH    CX
  41.  
  42. ; take care of zero as a special case -- if the decimal mantissa
  43. ;   is 0 then DX:AX are all set -- don't forget to clean off the
  44. ;   stack
  45.     OR    DX,DX
  46.     JNZ    L1
  47.     OR    AX,AX
  48.     JNZ    L1
  49.     POP    CX
  50.     RET
  51.  
  52. ; use SI to hold the binary exponent (which we'll start off unbiased --
  53. ;   the end result will have 23 digits after the 'binary' point) and
  54. ;   DI to hold the decimal exponent
  55. L1:    MOV    BINARYEXPONENT,23
  56.     MOV    DECIMALEXPONENT,BX
  57.     XOR    BX,BX
  58.  
  59. ; if the decimal exponent is negative, adjust it up to zero by a series
  60. ;   of divisions of the mantissa by 10
  61.     OR    DECIMALEXPONENT,DECIMALEXPONENT
  62.     JNS    L2
  63.  
  64. ; since we are going to be dividing the mantissa by 10, shift it left
  65. ;   as far as it will go so we won't lose any precision in the process
  66.     JMP    SHORT L3
  67. L4:    SHL    AX,1
  68.     RCL    DX,1
  69.     DEC    BINARYEXPONENT
  70. L3:    TEST    DX,8000h
  71.     JZ    L4
  72.     OR    AX,REMAINDER
  73.     CALL    LDIV10
  74.     INC    DECIMALEXPONENT
  75.     JS    L3
  76.     JZ    L5
  77.  
  78. ; if the decimal exponent is greater than zero, adjust it back to zero
  79. ;   by successive multiplication of the mantissa by 10 -- keep the
  80. ;   mantissa shifted far enough to the right that the multiplications
  81. ;   don't lose any of the top bits
  82. L6:    SHR    DX,1
  83.     RCR    AX,1
  84.     INC    BINARYEXPONENT
  85. L7:    TEST    DX,0F000h
  86.     JNZ    L6
  87.     MOV    CX,10
  88.     PUSH    AX
  89.     XCHG    DX,AX
  90.     MUL    CX
  91.     MOV    BX,AX
  92.     POP    AX
  93.     MUL    CX
  94.     ADD    DX,BX
  95.     DEC    DECIMALEXPONENT
  96. L2:    JNZ    L7
  97.  
  98. ; if the mantissa occupies too many positions shift it to the right,
  99. ;   rounding up as necessary as we go
  100.     JMP    SHORT L5
  101. L8:    SHR    DX,1
  102.     RCR    AX,1
  103.     ADC    AX,0
  104.     ADC    DX,0
  105.     INC    BINARYEXPONENT
  106. L5:    TEST    DX,0FF00h
  107.     JNZ    L8
  108.  
  109. ; if the mantissa is too far right, shift it to the left
  110.     JMP    SHORT L9
  111. L10:    SHL    AX,1
  112.     RCL    DX,1
  113.     DEC    BINARYEXPONENT
  114. L9:    TEST    DX,80h
  115.     JZ    L10
  116.  
  117. ; make the top bit invisible -- it's assumed
  118.     AND    DX,7Fh
  119.  
  120. ; bias the exponent, shift it left, and fold it in, stirring
  121. ;   gently for 20 minutes
  122.     ADD    BINARYEXPONENT,127
  123.     MOV    CL,7
  124.     SHL    BINARYEXPONENT,CL
  125.     OR    DX,BINARYEXPONENT
  126.  
  127. ; finally, get the sign into the top bit of DX
  128.     POP    CX
  129.     OR    CX,CX
  130.     JZ    L11
  131.     OR    DX,8000h
  132. L11:    RET
  133.  
  134. FDECTOBIN ENDP
  135.  
  136.         END
  137.